<template>
	<view>
		<!-- 收获地址组件 -->
		<!-- 1.选择收货地址的盒子 -->
		<div class="address-choose-box" v-if="JSON.stringify(address) === '{}'">
			<button type="primary" size="mini" class="btnChooseAddress" @click="chooseAddress">请选择收货地址</button>
		</div>
		<!--  2.渲染收货信息的盒子-->
		<div class="address-info-box" v-else @click="chooseAddress">
			<div class="row1">
				<div class="row1-left">
					<div class="username">收货人：<text>{{address.userName}}</text></div>
				</div>
				<div class="row1-right">
					<div class="phone">电话：<text>{{address.telNumber}}</text></div>
					<uni-icons type="arrowright" size="16"></uni-icons>
				</div>
			</div>
			<div class="row2">
				<div class="row2-left">收货地址：</div>
				<div class="row2-right">{{addstr}}</div>
			</div>
		</div>
		<!-- 底部边框线 -->
		<image src="/static/cart_border@2x.png" class="address-border"></image>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters,
	} from 'vuex'
	export default {
		name: "my-address",
		data() {
			return {

			};
		},
		methods: {
			// 把m_user模块中的updateAddress函数映射到当前组件
			...mapMutations('m_user', ['updateAddress']),
			// 选择收货地址
			async chooseAddress() {
				// 1.调用小程序提供的chooseAddress()方法，即可使用选择收获地址的功能
				// 返回值是一个数组，第一项为错误对象，第二项为成功之后的收获对象
				const [err, succ] = await uni.chooseAddress().catch(err => err)
				// 2.用户选择了收货地址
				if (err === null && succ.errMsg === 'chooseAddress:ok') {
					//console.log(succ);
					// this.address = succ
					this.updateAddress(succ)
				}
				console.log(err);
			}
		},
		computed: {
			//将m_user模块中的address对象映射当前组件中使用，代替data中的address对象
			...mapState('m_user', ['address']),
			//将m_user模块中的addstr映射到当前组件中使用
			...mapGetters('m_user', ['addstr']),

		},
	}
</script>

<style lang="scss">
	// 底部边框线样式
	.address-border {
		display: block;
		width: 100%;
		height: 5px;
	}

	// 选择收获地址的盒子
	.address-choose-box {
		display: flex;
		flex-direction: column;
		justify-content: center;
		font-size: 12px;
		height: 90px;
		padding: 0 5px;
	}

	// 渲染收货信息的盒子
	.address-info-box {
		font-size: 12px;
		height: 90px;
		display: flex;
		flex-direction: column;
		justify-content: center;
		padding: 0 5px;

		.row1 {
			display: flex;
			justify-content: space-between;

			.row1-left {

				.username {}
			}

			.row1-right {
				display: flex;
				align-items: center;

				.phone {
					margin-right: 5px;
				}
			}
		}

		.row2 {
			margin-top: 10px;
			display: flex;
			align-items: center;

			.row2-left {
				white-space: nowrap;
			}
		}
	}
</style>
